﻿#pragma once
#include <Windows.h>
#include <stdlib.h>
#include <time.h>

#include "CDxManager.h"
#include "CDInput.h"
#include "CTimer.h"


#define KEYBOARD_BUFFERED_SIZE 1024

#define SCREEN_640_480_24	1
#define SCREEN_800_600_24	2
#define	SCREEN_1024_768_24	3

#define SCREEN_640_480_32	4
#define	SCREEN_800_600_32	5
#define SCREEN_1024_768_32	6

#define KEY_DOWN(iKeyCode)		(IsKeyDown(iKeyCode))

class CGame
{
protected:

	//game's name
	//
	LPCSTR m_GameName;

	//backBuffer variables 
	//
	D3DFORMAT				m_BackBufferFormat;	//the backbuffer's format
	LPDIRECT3DSURFACE9		m_BackBuffer;		//the BackBuffer


	//Window's variables
	//
	HINSTANCE	m_hInstance;

	//Direct3D and KeyBoard's variables
	//
	CDxManager*				mp_Direct3D;
	BYTE					m_KeyBuffer[256];
	DIDEVICEOBJECTDATA		m_KeyEvents[KEYBOARD_BUFFERED_SIZE];
	//Screen's variables
	//
	int		mi_Mode;			//variable to set window's resolution
	int		mi_ScreenWidth;		//windows Width
	int		mi_ScreenHeight;	//Windows height
	int		mi_Depth;
	bool	mb_IsWindowed;		//variable to set FulScreen or Windowed property

	//FPS manage 
	int			mi_FPS;
	DWORD		mdw_DeltaTime;	

	//METHOD
	//
	void InitWindow(void);		//init game's window

	//set screen dimension
	//
	void SetScreenDimension(int iMode);
	//render a frame
	//
	void Render(void);

	//update KeyBoard
	//
	void UpdateKeyBoard(void);
	int IsKeyDown(int);
	//other methods
	//
	void StretchToSurface(IDirect3DSurface9*, RECT, RECT);
	LPDIRECT3DSURFACE9 GetBackBuffer();
	LPDIRECT3DSURFACE9 CreateSurfaceFromFile(LPCSTR FilePath);

	//virtual method
	//

	//init Direct Sound, each game will have a different sound, 
	//different sound file size, different SoundBuffer number
	//	
	virtual void LoadResource(void);
	virtual void RenderFrame(void);
	virtual void ProcessInput(void);
	virtual void UpdateFrame(void);

	virtual void OnKeyUp(int);
	virtual void OnKeyDown(int);

	static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

public:
	CGame(HINSTANCE hInstance, LPCSTR GameName, int iMode, bool bIsWindowed, int iFPS);
	~CGame();

	HWND		m_WndHandle;
	void Init(void);
	void Run(void);
	void Clear(void);

	int GetScreenWidth(void);
	int GetScreenHeight(void);
};
